<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="24">
          <el-form-item label="零件追溯件代码:" prop="partTrackNoId">
            <el-select v-model="formModel.partTrackNoId" clearable filterable>
              <el-option
                v-for="item in partTrackNoOptions"
                :key="item.partTrackNoId"
                :label="item.partTrackNo+'-'+item.partTrackNoName"
                :value="item.partTrackNoId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="零件物料编码:" prop="materialDataId">
            <el-select v-model="formModel.materialDataId" clearable filterable>
              <el-option
                v-for="item in materialDataOptions"
                :key="item.materialDataId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.materialDataId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否为沿用件:" prop="isUsed">
            <hey-select v-model="formModel.isUsed" :clearable="false" clcd="ind_cd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="formModel.isUsed==='1'" label="组件号:" prop="groupNumber">
            <el-input v-model="formModel.groupNumber" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <hey-select v-model="formModel.status" :clearable="false" clcd="is_effective_cd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'PartTrackGroupNumMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      tableData: [],
      materialDataOptions: [],
      partTrackNoOptions: [],
      formModel: {
        partTrackGroupNumId: '',
        partTrackNoId: '',
        materialDataId: '',
        isUsed: '0',
        groupNumber: '',
        status: '1'
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
    this.getPartTrackNoList()
    this.getMaterialDataList()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        partTrackNoId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        partNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        materialDataId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        isUsed: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        status: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    /** 查询物料主数据列表 */
    getMaterialDataList() {
      const param = { status: '1' }
      this.get('/materialData/listMaterialData', param).then(res => {
        this.materialDataOptions = res
      })
    },
    /** 查询零件追溯件代码 */
    getPartTrackNoList() {
      const param = { status: '1' }
      this.get('/partTrackNo/listPartTrackNo', param).then(res => {
        this.partTrackNoOptions = res
      })
    },
    /** 追溯件组号提交 */
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.partTrackGroupNumId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/partTrackGroupNum/update'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/partTrackGroupNum/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
